#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define MAX_SIZE 4


void basex_inc(unsigned int base, unsigned int *order, unsigned int size);

int main(int argc, char **argv) {
    char *group1;
    char *group2;
    unsigned int *len1;
    unsigned int *len2;
    char buf1[1000];
    char buf2[1000];
    unsigned int c1;
    unsigned int c2;
    double limit;
    
    unsigned int order[MAX_SIZE];
    
    unsigned int i;
    unsigned int j;
    unsigned int cursize;
    unsigned int list_len;
    
    FILE *in;
    
    
    if(argc < 2) {
        fprintf(stderr, "Error: No input file\n");
        return -1;
    }
    
    if(!(in = fopen(argv[1],"rb"))) {
        fprintf(stderr, "Error opening input file %s\n", argv[1]);
        return -1;
    }
    
    
    //_____Read words from file_______//
    
    fscanf(in, "%d\n", &list_len);
    
    len1 = malloc(list_len * sizeof(unsigned int));
    group1 = malloc(list_len * 256);
    
    for(i = 0; i < list_len; i++) {
        fscanf(in, "%s ", group1 + (256 * i));
        len1[i] = strlen(group1 + (256 * i));
    }
    
    
    fscanf(in, "\n");
    
    len2 = malloc(list_len * sizeof(unsigned int));
    group2 = malloc(list_len * 256);
    
    for(i = 0; i < list_len; i++) {
        fscanf(in, "%s ", group2 + (256 * i));
        len2[i] = strlen(group2 + (256 * i));
    }
    
    //______End file reading_______//
    
    
    //check the current solution
    for(cursize=1; cursize <= MAX_SIZE; cursize++) {
        //initialize the order
        memset(order, 0, MAX_SIZE * sizeof(unsigned int));
        
        limit = pow(4, cursize);
        
        for(j = 0; j < (unsigned int)limit; j++) {
            
            c1 = c2 = 0;
            
            for(i=0; i < cursize; i++) {
                strcpy(buf1 + c1, group1 + (256 * order[MAX_SIZE - i - 1]));
                c1 += len1[ order[MAX_SIZE - i - 1] ];
                strcpy(buf2 + c2, group2 + (256 * order[MAX_SIZE - i - 1]));
                c2 += len2[ order[MAX_SIZE - i - 1] ];
            }
            
            if(!strcmp(buf1, buf2)) {
                //solution found!
                
                for(i=0; i < MAX_SIZE; i++) {
                    printf("%d", order[MAX_SIZE - i - 1]);
                    if(i < MAX_SIZE - 1)
                        printf(", ");
                    
                }
                printf("\n");
                
                for(i=0; i < MAX_SIZE; i++) {
                    printf("%s", group1 + (256 * order[MAX_SIZE - i - 1]));
                }
                
                printf("\n");
                
                fclose(in);
                free(len1);
                free(len2);
                free(group1);
                free(group2);
                
                return 1;
            }
            
            //increment the base-x number by 1
            basex_inc(list_len, order, MAX_SIZE);
        }
    }
    
    //no solution found
    
    fclose(in);
    free(len1);
    free(len2);
    free(group1);
    free(group2);
    
    return 0;
}


void basex_inc(unsigned int base, unsigned int *order, unsigned int size) {
    int pos = size - 1;
    
    for(; pos >= 0; pos--) {
        order[pos]++;
        
        if(order[pos] > base - 1) {
            //carry
            order[pos] = 0;
        }
        else
            return;
    }
}


